package com.gamutsoft.amb.common.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class ExcelImportCheckUtil {
    
	/**
	 * 保留指定位数的小数
	 * 参数:String num 待验证数字,int length 保留的小数位数
	 */
	public static Double  parseDouble(String num,int length){
    	String fomat=".";
    	  for(int i=0;i<length;i++){
    		  fomat+="0";
    	  }
    	  DecimalFormat df = new DecimalFormat(fomat);
    	  Double d=Double.parseDouble(num);
    	  num=df.format(d);
    	  d=Double.parseDouble(num);
    	  return d;
    }
    
    //判断数字是正确(是否是正数/负数/All)
    
    /**
     * 判断数字是正确(是否是正数/负数/All)
     * 参数:String num 待验证数字,String type:待验证类型
     */
    public static boolean isValidDateNum(String num,String type){
		boolean result=false;
		 try {
	    	  Double data=Double.parseDouble(num);
	    	  if(">0".equals(type)){//>0
	    		  if(data>0){
	    			  result=true;
	    		  }
	    	  }else if(">=0".equals(type)){//>=0
	    		  if(data>=0){
	    			  result=true;
	    		  }
	    	  }else if("<0".equals(type)){
	    		  if(data<0){
	    			  result=true;
	    		  }
	    	  }else if("<=0".equals(type)){
	    		  if(data<=0){
	    			  result=true;
	    		  }
	    	  }else{
	    		  result=true;
	    	  }
	    	  
	    	 }catch (NumberFormatException e) {
	    	     return result;
	    	  }
	    	return result;
    }
    
    /**
     * 验证日期格式是否正确
     * 参数:String date 待验证日期,String format 待验证格式
     */
    public static  boolean isValidDate(String date,String format){
        boolean  result=false;
        try{
            SimpleDateFormat sdf = new SimpleDateFormat(format);
			if(date.equals(sdf.format(sdf.parse(date)))){
				result=true;
			}
        }catch(Exception e){
        	result=false;
        }
    	return result;
    }
    /**
     * 比较日期大小
     * 参数:String date1,String date2,type,format
     * 
     */
    public static boolean isValidDateTime(String date1,String date2,String type,String format){
    	 boolean  result=false;
    	 try{
    		 Calendar dateA = Calendar.getInstance();
 			 Calendar dateB = Calendar.getInstance();
 			 Date dateTimeA = (Date) new SimpleDateFormat(date1).parseObject(date1);
	         Date dateTimeB = (Date) new SimpleDateFormat(date2).parseObject(date2);
	         dateA.setTime(dateTimeA);
	         dateB.setTime(dateTimeB);
	         if(">".equals(type)){
	        	 return dateA.after(dateB);
	         }else if(">=".equals(type)){
	        	 return (dateA.after(dateB) || date1.equals(date2));
	         }else if("<".equals(type)){
	        	 return dateA.before(dateB);
	         }else if("<=".equals(type)){
	        	 return (dateA.before(dateB) || date1.equals(date2));
	         }
	         
    	 }catch(Exception e){
    		 result=false; 
    	 }
    	 return result;
    }
    
}
